package com.walk.zworld.ui.adapter

import android.view.LayoutInflater
import android.widget.TextView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.flexbox.FlexboxLayout
import com.walk.zworld.R
import com.base.lib.base.adapter.BaseSkeletonAdapter
import com.base.lib.httpUtils.KnowledgeTreeBody
import java.util.*

class SysKnowledgeAdapter : BaseSkeletonAdapter<KnowledgeTreeBody, BaseViewHolder>(R.layout.item_knowledge_tree_list){

    private var mInflater: LayoutInflater? = null
    private val mFlexItemTextViewCaches: Queue<TextView> = LinkedList()
    private var mOnItemClickListener: OnItemClickListener1? = null

    internal fun setOnItemClickListener1(onItemClickListener: OnItemClickListener1) {
        mOnItemClickListener = onItemClickListener
    }

    override fun convert(holder: BaseViewHolder, item: KnowledgeTreeBody) {
        holder.apply {
            holder.setText(R.id.tv_knowledge_item_title, item.name)
            val fbl: FlexboxLayout = holder.getView(R.id.fbl_knowledge)
            item.children.let {
                for (i in 0 until it.size) {
                    val childTextView = createOrGetCacheFlexItemTextView(fbl)
                    childTextView.text = it[i].name
                    childTextView.setOnClickListener {
                        mOnItemClickListener?.onClick(item, i)
                    }
                    fbl.addView(childTextView)
                }
            }
        }
    }

    private fun createOrGetCacheFlexItemTextView(fbl: FlexboxLayout): TextView {
        val tv = mFlexItemTextViewCaches.poll()
        return tv ?: createFlexItemTextView(fbl)
    }

    private fun createFlexItemTextView(fbl: FlexboxLayout): TextView {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(fbl.context)
        }
        return mInflater!!.inflate(R.layout.item_knowledge_text, fbl, false) as TextView
    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)
        val fbl = holder.getView<FlexboxLayout>(R.id.fbl_knowledge)
        for (i in 0 until fbl.childCount) {
            //offer:添加一个元素并返回true,如果队列已满,则返回false
            mFlexItemTextViewCaches.offer(fbl.getChildAt(i) as TextView)
        }
        fbl.removeAllViews()
    }

    interface OnItemClickListener1 {
        fun onClick(bean: KnowledgeTreeBody, pos: Int)
    }
}
